/**
 * @fileoverview Description of this file.
 */

var page = chrome.extension.getBackgroundPage();

var currentStation = null;
var playingFrame = null;
var playing = null;
var current_page = '';

var STATIONS = {'classical': [1389339, 1391497, 1421897, 1411742, 1399098],
                'pop': [1737843, 1407352, 1748767],
                'metal': [1722549, 1733339, 1733880, 1733877, 1388317]}

function stopPlay() {
  if (null !== playingFrame) {
    playingFrame.src = null;
  }
  playing = false;
}

function startPlay(url) {
  if (null === currentStation) {
    currentStation = url;
  };
  if (null === playingFrame) {
    playingFrame = document.createElement('iframe');
    document.body.appendChild(playingFrame);
  }
  playingFrame.style.display = "none";
  playingFrame.src = currentStation;
  playing = true;
}


function playStationById(station_id, callback) { 
  $.ajax({
    type: "GET",
    url: 'http://songza.com/api/1/station/' + station_id,
  }).fail(function(data, text, xhr) {
    console.log('failure searching for artist')
  }).success(function(data, text, xhr) {
    console.log('got station')
    console.log(data.url);
    currentStation = data.url;
    callback(currentStation);
  });
}

function getStationToPlay() {
  var stations;
  var genre;

  if (current_page.indexOf('facebook.com') >= 0) {
    genre = 'pop';
  } else if (current_page.indexOf('awics.corp.google.com') >= 0 ||
             current_page.indexOf('greentea.corp.google.com') >= 0) {
    genre = 'classical';  
  } else if (current_page.indexOf('memegen.googleplex.com') >= 0) {
    genre = 'metal'
  } else {
    genre = 'pop'
  }

  station = STATIONS[genre][Math.floor(Math.random()*STATIONS[genre].length)]
  playStationById(station, startPlay)
}

function urlChange(url) {
  if (undefined === url) {
    debugger;
  }
  console.log(url);
  current_page = url;

  if (current_page.indexOf('youtube.com') > 0 ||
      current_page.indexOf('staging.talkgadget.google.com') > 0) {
    stopPlay();
    setPlayIcon(); 
  }
}

// two situations that cause change of view - tab switch, and navigation within tabs
// within tab
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, updatedTab) {
  chrome.tabs.query({active: true}, function(currentTabs) {
    var matchingTabs = currentTabs.filter(function(t) {return updatedTab.id === t.id;}); 
    if (matchingTabs.length > 0) {
      if (undefined !== changeInfo.url) {
        var newUrl = changeInfo.url;
        urlChange(newUrl);
      }
    }
  });
});

// tab switch
chrome.tabs.onActiveChanged.addListener(function(tabId) {
  chrome.tabs.onUpdated.addListener
  chrome.tabs.get(tabId, function(tab) {
    var newUrl = tab.url;
    urlChange(newUrl);
  });
})

/*
 * Portion that deals with play, stoppage of play
 */

function setStopIcon() {
  chrome.browserAction.setIcon({path: 'images/pause.png'});
}

function setPlayIcon() {
  chrome.browserAction.setIcon({path: 'images/run.png'});
}

function playToggle() {
  if (!playing) {
    getStationToPlay();
    setStopIcon();
  } else {
    stopPlay();
    setPlayIcon();
  }
}

chrome.browserAction.onClicked.addListener(function(tab) {
  playToggle()
});
